package com.jdcjk.project.login.presenter;

import android.content.Context;

import com.google.gson.Gson;
import com.jdcjk.base.presenter.BasePresenter;
import com.jdcjk.project.login.model.LoginModel;
import com.jdcjk.project.login.model.entity.LoginResult;
import com.zhy.http.okhttp.callback.StringCallback;

import okhttp3.Call;
import okhttp3.Request;

/**
 * Created by Administrator on 2016/12/13 0013.
 * 登录程序中介类
 */
public class LoginPresenter extends BasePresenter<LoginModel> {
    private final String TAG = LoginPresenter.class.getSimpleName();

    public LoginPresenter(Context context) {
        super(context);
    }

    @Override
    public LoginModel bindModel() {
        return new LoginModel(getContext());
    }

    public void login(String url, String account, String password, final OnUIThreadListener<LoginResult> onUIThreadListener) {
        getModel().login(url, account, password, new StringCallback() {
            @Override
            public void onBefore(Request request, int id) {
                onUIThreadListener.onBefore(request, id);
            }

            @Override
            public void onAfter(int id) {
                onUIThreadListener.onAfter(id);
            }

            @Override
            public void onError(Call call, Exception e, int i) {
                onUIThreadListener.onError(call, e, i);
            }

            @Override
            public void onResponse(String s, int i) {
                Gson json = new Gson();
                LoginResult loginResult = json.fromJson(s, LoginResult.class);
                onUIThreadListener.onResponse(loginResult);
            }
        });
    }

}
